package sunlight.validation;

public class cnpValidator {
 
	public static int[] getArray(String cnp){
		int[] cnpArray = new int[13];
		
		for(int i=0; i<cnp.length(); i++){
			cnpArray[i] = Integer.parseInt(String.valueOf(cnp.charAt(i)));
		}
		
		return cnpArray;
	}
	
	public static boolean is_valid(String cnp){
		if(cnp.length() == 13){
			int[] cnpArray = cnpValidator.getArray(cnp);
			
			long sum = cnpArray[0] * 2 + cnpArray[1] * 7 + cnpArray[2] * 9 + cnpArray[3] * 1 + cnpArray[4] * 4 + cnpArray[5] * 6 
					 + cnpArray[6] * 3 + cnpArray[7] * 5 + cnpArray[8] * 8 + cnpArray[9] * 2 + cnpArray[10] * 7 + cnpArray[11] * 9;
			
			long rest = sum % 11;
			
			if(((rest < 10) && (rest == cnpArray[12])) || ((rest == 10) && (cnpArray[12] == 1))){
				return true;
			}
		}
		
		return false;
	}
	
	
	public static int[] getBirthdate(String cnp){
		int[] cnpArray = cnpValidator.getArray(cnp);
		
		String day 		= String.valueOf(cnpArray[5]) + String.valueOf(cnpArray[6]);
		String month	= String.valueOf(cnpArray[3]) + String.valueOf(cnpArray[4]);
		String year		= "19" + String.valueOf(cnpArray[1]) + String.valueOf(cnpArray[2]);
		
		int iday 	= Integer.parseInt(day);
		int imonth 	= Integer.parseInt(month);
		int iyear	= Integer.parseInt(year);
	
		return new int[]{iday, imonth, iyear};
	}
	
}
